package com.allen.retrofit2_rxjava2.view.selectimageview.utils;

import java.io.File;

import com.nostra13.universalimageloader.cache.disc.DiscCacheAware;
import com.nostra13.universalimageloader.cache.disc.impl.LimitedAgeDiscCache;
import com.nostra13.universalimageloader.cache.disc.naming.Md5FileNameGenerator;
import com.nostra13.universalimageloader.core.DefaultConfigurationFactory;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.assist.ImageLoadingListener;
import com.nostra13.universalimageloader.core.assist.ImageLoadingProgressListener;
import com.nostra13.universalimageloader.core.assist.ImageSize;
import com.nostra13.universalimageloader.core.assist.SimpleImageLoadingListener;
import com.nostra13.universalimageloader.core.display.RoundedBitmapDisplayer;
import com.nostra13.universalimageloader.core.display.SimpleBitmapDisplayer;
import com.nostra13.universalimageloader.core.imageaware.ImageAware;
import com.nostra13.universalimageloader.utils.StorageUtils;

import android.content.Context;
import android.graphics.Bitmap;
import android.widget.ImageView;

/**    
 * 图片加载工具类
 * 
 * @author guilin   
 */
public class UniversalImageLoadTool {

	private static ImageLoader imageLoader = ImageLoader.getInstance();
	
	/**
	 * 获取{@link ImageLoader}
	 * @return
	 */
	public static ImageLoader getImageLoader(){
		return imageLoader;
	}
	/**
	 * 检查 {@linkplain #init(ImageLoaderConfiguration) is initialized with
	 * configuration};
	 */
	public static boolean checkImageLoader(){
		return imageLoader.isInited();
	}
	
	/**
	 * 加载image
	 * @param uri
	 * @param imageView
	 * @param default_pic
	 */
	public static void disPlay(String uri, ImageView imageView,int default_pic){
		DisplayImageOptions options = new DisplayImageOptions.Builder()
		.showImageOnLoading(default_pic)
		.showImageForEmptyUri(default_pic)
		.showImageOnFail(default_pic)
		.cacheInMemory(true)
		.cacheOnDisc(true)
		.bitmapConfig(Bitmap.Config.RGB_565)
		.displayer(new SimpleBitmapDisplayer())
		.build();

		imageLoader.displayImage(uri, imageView, options);
	}
	
	/**
	 * 加载image <br>
	 * {@link SimpleImageLoadingListener} 加载监听
	 * @param uri
	 * @param imageView
	 * @param default_pic
	 * 
	 */
	public static void disPlay(String uri, ImageView imageView,int default_pic,SimpleImageLoadingListener simpleImageLoadingListener){
		DisplayImageOptions options = new DisplayImageOptions.Builder()
		.showImageOnLoading(default_pic)
		.showImageForEmptyUri(default_pic)
		.showImageOnFail(default_pic)
		.cacheInMemory(true)
		.cacheOnDisc(true)
		.bitmapConfig(Bitmap.Config.RGB_565)
		.displayer(new SimpleBitmapDisplayer())
		.build();
		imageLoader.displayImage(uri, imageView, options,simpleImageLoadingListener);
	}
	
	/**
	 * 加载image <br>
	 * {@link SimpleImageLoadingListener}加载监听<br>
	 * {@link ImageLoadingProgressListener} 加载进度监听
	 * @param uri
	 * @param imageView
	 * @param default_pic
	 * @param simpleImageLoadingListener
	 * @param ImageLoadingProgressListener
	 */
	public static void disPlay(String uri, ImageView imageView,int default_pic,
			SimpleImageLoadingListener simpleImageLoadingListener,ImageLoadingProgressListener imageLoadingProgressListener){
		DisplayImageOptions options = new DisplayImageOptions.Builder()
		.showImageOnLoading(default_pic)
		.showImageForEmptyUri(default_pic)
		.showImageOnFail(default_pic)
		.cacheInMemory(true)
		.cacheOnDisc(true)
		.bitmapConfig(Bitmap.Config.RGB_565)
		.displayer(new SimpleBitmapDisplayer())
		.build();

		imageLoader.displayImage(uri, imageView, options,simpleImageLoadingListener,imageLoadingProgressListener);
	}

	/**
	 * 自定义{@link DisplayImageOptions}加载image
	 * @param uri
	 * @param imageView
	 * @param options
	 */
	public static void disPlayDefined(String uri, ImageView imageView,DisplayImageOptions options){
		imageLoader.displayImage(uri, imageView, options);
	}
	
	/**
	 * 加载image
	 * @param uri
	 * @param imageAware
	 * @param default_pic
	 */
	public static void disPlay(String uri, ImageAware imageAware,int default_pic){
		DisplayImageOptions options = new DisplayImageOptions.Builder()
		.showImageOnLoading(default_pic)
		.showImageForEmptyUri(default_pic)
		.showImageOnFail(default_pic)
		.cacheInMemory(true)
		.cacheOnDisc(true)
		.bitmapConfig(Bitmap.Config.RGB_565)
		.displayer(new SimpleBitmapDisplayer())
		.build();

		imageLoader.displayImage(uri, imageAware, options);
	}
	
	/**
	 * 加载圆角image
	 * @param uri
	 * @param imageView
	 * @param default_pic
	 */
	public static void disPlayRound(String uri, ImageView imageView,int default_pic){
		DisplayImageOptions options = new DisplayImageOptions.Builder()
		.showImageOnLoading(default_pic)
		.showImageForEmptyUri(default_pic)
		.showImageOnFail(default_pic)
		.cacheInMemory(true)
		.cacheOnDisc(true)
		.displayer(new RoundedBitmapDisplayer(20))
		.bitmapConfig(Bitmap.Config.RGB_565)
		.displayer(new SimpleBitmapDisplayer())
		.build();

		imageLoader.displayImage(uri, imageView, options);
	}
	
	/**
	 * 下载image
	 * @param uri
	 */
	public static void loadImage(String uri){
		DisplayImageOptions options = new DisplayImageOptions.Builder()
		.cacheInMemory(true)
		.cacheOnDisc(true)
		.bitmapConfig(Bitmap.Config.RGB_565)
		.displayer(new SimpleBitmapDisplayer())
		.build();
		imageLoader.loadImage(uri,options,null);
	}
	
	/**
	 * 下载图片<br>
	 * {@link ImageLoadingListener}加载监听
	 * @param uri
	 * @param imageLoadingListener
	 */
	public static void loadImage(String uri,ImageLoadingListener imageLoadingListener){
		DisplayImageOptions options = new DisplayImageOptions.Builder()
		.cacheInMemory(true)
		.cacheOnDisc(true)
		.bitmapConfig(Bitmap.Config.RGB_565)
		.displayer(new SimpleBitmapDisplayer())
		.build();
		imageLoader.loadImage(uri, options, imageLoadingListener);
	}
	
	/**
	 * 下载图片<br>
	 * {@link ImageLoadingListener}加载监听<br>
	 * {@link ImageSize}图片大小
	 * @param uri
	 * @param imageLoadingListener
	 */
	public static void loadImage(String uri,ImageSize targetImageSize,ImageLoadingListener imageLoadingListener){
		imageLoader.loadImage(uri, targetImageSize, imageLoadingListener);
	}
	
	/**
	 *  下载图片<br>
	 * {@link ImageLoadingListener}加载监听<br>
	 * {@link ImageLoadingProgressListener}加载进度监听
	 * @param uri
	 * @param imageLoadingListener
	 */
	public static void loadImage(String uri,ImageLoadingListener imageLoadingListener,
			ImageLoadingProgressListener imageLoadingProgressListener){
		DisplayImageOptions options = new DisplayImageOptions.Builder()
		.cacheInMemory(true)
		.cacheOnDisc(true)
		.bitmapConfig(Bitmap.Config.RGB_565)
		.displayer(new SimpleBitmapDisplayer())
		.build();
		imageLoader.loadImage(uri, null, options, imageLoadingListener, imageLoadingProgressListener);
	}
	
	
	/**
	 * 重命名
	 * @param uri
	 * @param targetUri
	 */
	public static void renameOnDiscCache(Context context,String uri,String targetUri){
		DiscCacheAware reserveDiscCache = DefaultConfigurationFactory.createReserveDiscCache(new File(targetUri));
		DiscCacheAware discCache  = new LimitedAgeDiscCache(StorageUtils.getCacheDirectory(context),new Md5FileNameGenerator(), 3600*24*15L);
		File imageFile = discCache.get(uri);
		File imageTargetFile = discCache.get(targetUri);
		File cacheDir = imageFile.getParentFile();
		if (cacheDir == null || (!cacheDir.exists() && !cacheDir.mkdirs())) {
			imageFile = reserveDiscCache.get(uri);
			imageTargetFile = reserveDiscCache.get(targetUri);
			cacheDir = imageFile.getParentFile();
			if (cacheDir != null && !cacheDir.exists()) {
				cacheDir.mkdirs();
			}
		}
		imageFile.renameTo(imageTargetFile);
	}

	/**
	 * 清除缓存
	 */
	public static void clear(){
		imageLoader.clearMemoryCache();		
		imageLoader.clearDiscCache();
	}
	/**
	 * 重启加载
	 */
	public static void resume(){
		imageLoader.resume();
	}
	/**
	 * 暂停加载
	 */
	public static void pause(){
		imageLoader.pause();
	}
	/**
	 * 停止加载
	 */
	public static void stop(){
		imageLoader.stop();
	}
	/**
	 * 销毁加载
	 */
	public static void destroy() {
		imageLoader.destroy();
	}
}
